package org.jeecg.modules.business.service.impl;

import cn.hutool.core.date.DateTime;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.entity.BusAgreementOrderFee;
import org.jeecg.modules.business.entity.BusMarketAgreementUnit;
import org.jeecg.modules.business.enums.AgreementOrderStatusEnum;
import org.jeecg.modules.business.mapper.BusMarketAgreementUnitMapper;
import org.jeecg.modules.business.service.IBusMarketAgreementUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 协议单位
 * @Author: jeecg-boot
 * @Date:   2023-03-25
 * @Version: V1.0
 */
@Service
public class BusMarketMarketAgreementUnitServiceImpl extends ServiceImpl<BusMarketAgreementUnitMapper, BusMarketAgreementUnit> implements IBusMarketAgreementUnitService {
    @Autowired
    BusMarketAgreementUnitMapper busMarketAgreementUnitMapper;
    /**
     * 获取最大账号
     */
    @Override
    public String getMaxAccount() {
        String maxAccount = busMarketAgreementUnitMapper.queryMaxAccount();
        return  maxAccount;
    }

    @Override
    public BusAgreementOrderFee agreementPayFee(BusMarketAgreementUnit marketAgreementUnit, BigDecimal money, String feeId, String hotelId){
        // 计算余额与账单金额差价，如果差价小于0，将余额置0，将差价加到已用额度。差价大于等于0，直接扣余额
        BigDecimal differMoney = marketAgreementUnit.getBalance().subtract(money);
        if (differMoney.compareTo(new BigDecimal(0)) < 0){
            marketAgreementUnit.setBalance(new BigDecimal(0));
            marketAgreementUnit.setAmountUsed(marketAgreementUnit.getAmountUsed().subtract(differMoney));
        } else {
            marketAgreementUnit.setBalance(differMoney);
        }
        // 已用额度大于记账额度，抛出异常
        if (marketAgreementUnit.getBookkeeping() != null
                && (marketAgreementUnit.getAmountUsed().compareTo(marketAgreementUnit.getBookkeeping()) > 0)){
            throw new JeecgBootException(marketAgreementUnit.getCustomerName() + "超过记账额度");
        }
        return addAgreementOrderFee(feeId, money, marketAgreementUnit.getId(), true);
    }

    public BusAgreementOrderFee addAgreementOrderFee(String feeId, BigDecimal money, String agreeUnitId, Boolean isCollect){
        LoginUser user = TokenUtils.getAuthUser();
        BusAgreementOrderFee agreementOrderFee = new BusAgreementOrderFee();
        agreementOrderFee.setAgreementUnitId(agreeUnitId);
        agreementOrderFee.setCreateUserId(user.getId());
        agreementOrderFee.setOrderFeeId(feeId);
        //生成订单号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        agreementOrderFee.setOrderNo("R"+sdf.format(System.currentTimeMillis()));
        agreementOrderFee.setStatus(AgreementOrderStatusEnum.NOT_SETTLEMENT.getKey());
        // 商品消费
        agreementOrderFee.setType(3);
        if (isCollect){
            agreementOrderFee.setPayPrice(money);
            agreementOrderFee.setProjectName("收款");
        } else {
            agreementOrderFee.setProjectName("退款");
            agreementOrderFee.setPayPrice(money.multiply(new BigDecimal(-1)));
        }
        agreementOrderFee.setCreatedTime(new DateTime());
        return agreementOrderFee;
    }

}
